package cn.edu.bupt.ch3_1.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncTestService {

    public void syncMethod() {
        this.timeConsuming();
    }

    @Async
    public void asyncMethod() {
        this.timeConsuming();
    }

    private void timeConsuming() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
                System.out.println("主线程名称：" + Thread.currentThread().getName() + " i:" + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("主线程名称：" + Thread.currentThread().getName() + " work is done");
    }
}
